#include "searchWordWidget.h"

char eng[9][4]=
{
	{'.','@',',',':'},
	{'a','b','c','2'},
	{'d','e','f','3'},
	{'g','h','i','4'},
	{'j','k','l','5'},
	{'m','n','o','6'},
	{'p','q','r','s'},
	{'t','u','v','8'},
	{'w','x','y','z'}
};
char engb[9][4]=
{
	{'.','@',',',':'},
	{'A','B','C','2'},
	{'D','E','F','3'},
	{'G','H','I','4'},
	{'J','K','L','5'},
	{'M','N','O','6'},
	{'P','Q','R','S'},
	{'T','U','V','8'},
	{'W','X','Y','Z'}
};
searchWordWidget::searchWordWidget(QWidget *parent):ZWidget(parent)
{
	engtimer = new QTimer(this);
	connect(engtimer,SIGNAL(timeout()), this, SLOT(timeDone()));
	sensitivity=300;
	engx=0;engy=0;oldx=0;oldy=0;
	sta=engLow;//小写
	tmpstr=QString("");
	vOffset = 0;
	openKdict();
}
searchWordWidget::~searchWordWidget()
{
	delete engtimer;
}

void searchWordWidget::openKdict()
{
	initkdic();
	openkdic("/mmc/mmca1/qttest/dict/Oxford_for_zdic.pdb");
	engStr = QString("");
	updateMean();
}

void searchWordWidget::updateMean()
{
	char *eng;
	eng = searchword_general_kdic(un_unic(engStr), -1);
	meanStr = un_gbk(eng);
	for(unsigned int i=0; i<meanStr.length()-1; i++)
	{
		QChar a = meanStr.at(i);
		if(a=='\\' && meanStr.at(i+1) == 'n') 
		{
			QString s=QString("\n");
			meanStr.replace(i,1,s);
			meanStr.remove(i+1,1);
		}
	}
	vOffset=0;
	this->repaint();
}

void searchWordWidget::paintEvent(QPaintEvent *e)
{
	e=e;
	p = QPixmap(this->width(), this->height());
	QPainter *pa = new QPainter();
	ZText zt = ZText(meanStr);
	QFont font = QFont("SSong");
	font.setPointSize(15);
	textHeight = zt.getWrapTextHeight(font, MEAN_HEIGHT, ZWrapLine::AtWhiteSpace);

	/* 画出翻译文本 */
	QPixmap textPix = QPixmap(MEAN_HEIGHT, textHeight);
	QRect rect = QRect(0, 0, MEAN_HEIGHT, textHeight);
	textPix.fill(QColor(255,255,255));
	pa->begin(&textPix);
		pa->setFont(font);
		zt.paint(*pa, rect, true, Qt::AlignLeft | Qt::AlignTop, QString("..."), 2, ZWrapLine::AtWhiteSpace);
	pa->end();
		

	QFont ori_font = pa->font();
	ori_font.setBold(true);
	ori_font.setPointSize(18);
	/* 画出英语 */
	p.fill(QColor(255,255,255));
	pa->begin(&p);
		pa->setFont(ori_font);
		pa->setPen(QColor(0,0,255));
		pa->drawText(0, 20, engStr);
		pa->setPen(QColor(0, 0, 0));
		//pa->drawText(0,50, phoneticStr);
		pa->drawLine(0,MEAN_Y - 1,240,MEAN_Y - 1);
		/* 画出进度条 */
		pa->setPen(QColor(0,255,0));
		pa->drawLine(237,MEAN_Y,237, MEAN_Y+MEAN_HEIGHT);
		pa->drawLine(239,MEAN_Y,239, MEAN_Y+MEAN_HEIGHT);
		pa->setPen(QColor(0,0,255));
		float per;
		if(textHeight < MEAN_HEIGHT)
			per = 1.0;
		else
			per = (vOffset*1.0/(textHeight-MEAN_HEIGHT));
		//printf("percentage = %f\n", per);
		int lenPercent = MEAN_HEIGHT*per;
		//printf("lenPercent = %d\n", lenPercent);
		pa->drawLine(238,MEAN_Y,238, MEAN_Y+lenPercent);

		//画底线
		pa->setPen(QColor(0,0,0));
		pa->drawLine(0, MEAN_Y + MEAN_HEIGHT+1, 240, MEAN_Y + MEAN_HEIGHT+1);
	pa->end();
	delete pa;

	/* 将翻译复制至buffer中 */
	bitBlt(&p, 0, MEAN_Y, &textPix, 0, vOffset, MEAN_WIDTH, MEAN_HEIGHT-2);

	/* 将buffer显示到屏幕 */
	bitBlt(this,0,0,&p);
}

void searchWordWidget::keyPressEvent(QKeyEvent *e)
{
	int kvalue = e->key();
	if(kvalue == KEY_DOWN)
	{
		if(textHeight <= MEAN_HEIGHT) return;
		if(vOffset+OFFSET_STEP < textHeight-MEAN_HEIGHT)
			vOffset+=OFFSET_STEP;
		else
			vOffset = textHeight-MEAN_HEIGHT;
		this->repaint();
		return;
	}
	if(kvalue == KEY_UP)
	{
		if(vOffset-OFFSET_STEP>= 0)
			vOffset-=OFFSET_STEP;
		else
			vOffset=0;
		this->repaint();
		return;
	}
	if(engtimer->isActive())//如果时钟是活动的
	{
		if(kvalue>=KEY_1&&kvalue<=KEY_9)
		{
			unsigned int tmp_t=kvalue-48;
			engx=tmp_t-1;//对应的数字
			if(engx!=oldx)//按下了新键
			{
				engy=0;
				if(sta == engLow) tmpstr+=eng[oldx][oldy];
				else tmpstr+=engb[oldx][oldy];
				engtimer->stop();
				engtimer->start(sensitivity);
				oldx=engx;oldy=engy;
				imeOutput(tmpstr);
			}
			else
			{
				engtimer->stop();
				if(engy==3) engy=0;
				else engy++;
				oldy=engy;oldx=engx;
				engtimer->start(sensitivity);
			}
		}
		else
		{
			engy=0;
			if(sta == engLow) tmpstr+=eng[oldx][oldy];
			else tmpstr+=engb[oldx][oldy];
			engtimer->stop();
			imeOutput(tmpstr);

			if(kvalue==KEY_0)
			{
				tmpstr+=" ";
				imeOutput(tmpstr);
			}
			else if(kvalue==KEY_SPOND)
			{
				if(sta == engLow)
					sta=engUp;
				else
					sta=engLow;
				engtimer->stop();
			}
			else if(kvalue==KEY_CLEAR)//C键
			{

			}
		}
	}
	else//否则输入新东西
	{
		if(kvalue>=KEY_1&&kvalue<=KEY_9)
		{
			engtimer->start(sensitivity);
			unsigned int tmp_t=kvalue-48;
			engx=tmp_t-1;//对应的数字
			oldx=engx;oldy=0;
		}
		else if(kvalue==KEY_0)
		{
			tmpstr+=" ";
			imeOutput(tmpstr);
		}
		else if(kvalue==KEY_CENTER)
		{
			if(sta == engLow)
				sta=engUp;
			else
				sta=engLow;
			engtimer->stop();
		}
		else if(kvalue==KEY_SPOND)
		{
			if(sta == engLow)
				sta=engUp;
			else
				sta=engLow;
			engtimer->stop();
		}
		else if(kvalue==KEY_CLEAR || kvalue==4099)//C键
		{
			imeOutput("delete");
			return;
		}
	}
}

void searchWordWidget::timeDone()
{
	if(sta==engLow) tmpstr+=eng[engx][engy];
	else tmpstr+=engb[engx][engy];
	engy=0;
	imeOutput(tmpstr);
	engtimer->stop();
}

void searchWordWidget::imeOutput(QString str)
{
	tmpstr=QString("");
	/*改变engStr的值，并刷新翻译*/
	if(str == cntr("delete")) engStr.remove(engStr.length()-1,1);
	else engStr+=str;
	updateMean();
	this->repaint();
}

void searchWordWidget::clearEngStr()
{
	engStr=QString("");
	updateMean();
}

void searchWordWidget::addCurrentWord()
{
	if(engStr.length() == 0)
	{
		printf("ERROR: can not add empty words!!!!\n");
		return;
	}
	int total;
	PYDB *cetdb = new PYDB("/mmc/mmca1/qttest/dict/unKnowWords.db");
	cetdb->find(QString("configs"), QString("key"), QString("total"));
	total = cetdb->getresult().toInt();
	cetdb->add(QString("engs"), QString("%1").arg(total), QString("%1").arg(engStr));
	total++;
	cetdb->update(QString("configs"), QString("key"), QString("total"), QString("value"), QString("%1").arg(total));
}

